package com.wishu.houses.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.wishu.houses.domain.TbPremisesInfo;
import com.wishu.houses.service.SysRegionService;
import com.wishu.houses.service.TbPremisesInfoService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("houses")
public class HousesController {
    @Resource
    private TbPremisesInfoService premisesInfoService;

    @Resource
    private SysRegionService sysRegionService;

    /**
     * 楼盘列表
     * @param page 分页对象
     * @param tbPremisesInfo 实体对象
     * @return
     */
    @RequiresPermissions("houses:houses:list")
    @RequestMapping("list")
    //@RequiresPermissions("wishu-houses/houses/list")
    public Map<String,Object> list(Page page,TbPremisesInfo tbPremisesInfo){
        Map<String, Object> map=new HashMap<>();
        IPage<TbPremisesInfo> list = premisesInfoService.selectpage(page,tbPremisesInfo);
        map.put("list", list);
        map.put("code", 200);
        return map;
    }

    /**
     * 获取地址
     * @return
     */
    @RequiresPermissions("houses:houses:treeRegion")
    @RequestMapping("treeRegion")
    public Map<String,Object> treeRegion(){
        Map<String, Object> map=new HashMap<>();
        List<Tree<String>> trees = sysRegionService.treeRegion();
        map.put("list", trees);
        map.put("code", 200);
        return map;
    }


    /**
     * 楼盘添加
     * @param premisesInfo 实体对象
     * @return
     */
    @RequiresPermissions("houses:houses:save")
    @RequestMapping("/save")
    //@RequiresPermissions("houses/houses/save")
    public Map<String,Object> save(@RequestBody TbPremisesInfo premisesInfo) {
        Map<String, Object> map = new HashMap<>();
        boolean save = premisesInfoService.save(premisesInfo);
        if (save){
            map.put("code",200);
            map.put("msg","提交成功");
        }else {
            map.put("code",777);
            map.put("msg","提交失败");
        }
        return map;
    }
}
